package se.webbzon.boltzmann.minigame.botany;

import java.awt.Rectangle;

import se.webbzon.boltzmann.configuration.Difficulty;
import se.webbzon.boltzmann.minigame.chemistry.ArgonMolecule;
import se.webbzon.boltzmann.minigame.chemistry.HydrogenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.MonatomicOxygenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.NitrogenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.OxygenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.OzoneMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SandBoxMolecule;
import se.webbzon.boltzmann.minigame.chemistry.WaterMolecule;
import se.webbzon.boltzmann.minigame.effect.SandBoxCloud;
import se.webbzon.boltzmann.minigame.effect.SandBoxSun;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxAnchor;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxContainer;
import se.webbzon.boltzmann.minigame.sandbox.ReadOnlySandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxArrow;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxPicture;
import se.webbzon.boltzmann.resource.Resources;

public class OzoneCycle extends Cycle {
	
	/*============================================================
	Protected Methods
	============================================================*/

	@Override protected void onCreate(ReadOnlySandBox box) {
		Rectangle r = new Rectangle();
		box.getBoundary(r);
		double x0 = 0, y0 = 0; //r.getMinX(), y0 = r.getMinY();
		double w = r.getWidth(), h = r.getHeight();
		
		/** Create instances **/
		DefaultSandBoxContainer o2, o, o3;
		o2 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		o2.setPosition(x0 + 64, y0 + 132);
		o = new DefaultSandBoxContainer(SandBoxMolecule.class);
		o.setPosition(x0 + 312, y0 + 132);
		o3 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		o3.setPosition(x0 + 576, y0 + 64);
		
		SandBoxPicture exo1, exo2, exo3, po, po2, po3;
		exo1 = new SandBoxPicture(Resources.getImageSet("chemistry_exothermic"),50);
		exo1.setSize(48, 48);
		exo1.setPosition(x0 + 504, y0 + 132);
		exo2 = new SandBoxPicture(Resources.getImageSet("chemistry_exothermic"),50);
		exo2.setSize(48, 48);
		exo2.setPosition(x0 + 504, y0 + 200);
		exo3 = new SandBoxPicture(Resources.getImageSet("chemistry_exothermic"),50);
		exo3.setSize(48, 48);
		exo3.setPosition(x0 + 504, y0 + 64);
		po = new SandBoxPicture(Resources.getImageSet("chemistry_o"),50);
		po.setSize(12, 12);
		po.setPosition(x0 + 440, y0 + 100);
		po2 = new SandBoxPicture(Resources.getImageSet("chemistry_o2"),50);
		po2.setSize(24, 24);
		po2.setPosition(x0 + 440, y0 + 32);
		po3 = new SandBoxPicture(Resources.getImageSet("chemistry_o3"),50);
		po3.setSize(32, 32);
		po3.setPosition(x0 + 440, y0 + 168);
		
		SandBoxSun sun = new SandBoxSun();
		sun.setPosition(x0 + 40, y0 + 40);
		sun.setHradius(0);
		sun.setVradius(0);
		sun.setDeltaTime(0);
		SandBoxCloud cloud = new SandBoxCloud();
		cloud.setPosition(x0 + r.getCenterX(), y0 + 40);
		cloud.setSize(r.getWidth(), 48);
		cloud.setIntensity(2);
		Earth earth = new Earth();
		earth.setSize(earth.getWidth(), 16);
		
		/** Create arrows and anchors**/
		DefaultSandBoxAnchor an1, an2, an3, an4, an5, an6;
		an1 = new DefaultSandBoxAnchor(x0 + 320, y0 + 64);
		an2 = new DefaultSandBoxAnchor(x0 + 320, y0 + 200);
		an3 = new DefaultSandBoxAnchor(x0 + 560, y0 + 132);
		an4 = new DefaultSandBoxAnchor(x0 + 560, y0 + 200);
		an5 = new DefaultSandBoxAnchor(x0 + 560, y0 + 232);
		an6 = new DefaultSandBoxAnchor(x0 + 64, y0 + 232);
		
		SandBoxArrow[] arrows = {
				new SandBoxArrow(o2,o),
				new SandBoxArrow(o,exo3),
				new SandBoxArrow(o,exo1),
				new SandBoxArrow(o,exo2),
				new SandBoxArrow(exo1,o2),
				new SandBoxArrow(exo2,o2),
				new SandBoxArrow(sun,o),
				new SandBoxArrow(po,exo1),
				new SandBoxArrow(po2,exo3),
				new SandBoxArrow(po3,exo2),
				new SandBoxArrow(exo3,o3),
		};
		arrows[1].addAnchor(an1);
		arrows[3].addAnchor(an2);
		arrows[4].addAnchor(an3);
		arrows[4].addAnchor(an5);
		arrows[4].addAnchor(an6);
		arrows[5].addAnchor(an4);
		arrows[5].addAnchor(an5);
		arrows[5].addAnchor(an6);
		
		/** Add items to cycle **/
		SandBoxItem[] items = {
				o2, o, o3,
				sun, cloud, earth,
				exo1, exo2, exo3, 
				po, po2, po3
		};
		
		for (SandBoxItem item : items)
			addItem(item);
		for (SandBoxItem item : arrows)
			addItem(item);
		
		/** Create and add molecules **/
		SandBoxMolecule[] molecules = {
				new WaterMolecule(), new ArgonMolecule(),
				new OxygenMolecule(), new MonatomicOxygenMolecule(),
				new OzoneMolecule(), new HydrogenMolecule(),
				new NitrogenMolecule()
		};
		
		for (SandBoxMolecule m : molecules) {
			m.setPosition(	x0 + (0.1 + 0.4 * w * Math.random()), 
							y0 + (0.1 + 0.4 * h * Math.random()));
			m.setLifetime(-1);
			addItem(m);
		}
		
		/** Add solutions **/
		addSolution(o2,OxygenMolecule.class);
		addSolution(o,MonatomicOxygenMolecule.class);
		addSolution(o3,OzoneMolecule.class);
	}

	@Override protected void onDestroy(ReadOnlySandBox box) {
		clearSolution();
		clearItems();
	}
	
	@Override public Difficulty getDifficulty() {
		return Difficulty.EASY;
	}

}
